import moment from "moment";

// 获取昨天的日期(天)
export const getYesterday = (num = 1) => {
    // 获取当前时间
    const today = moment();

    // 获取昨天的日期
    const yesterday = today.subtract(num, "day").format("YYYY-MM-DD");

    return yesterday;
};

// 获取当前月的日期
export const getCurYearMonth = () => {
    // 获取当前时间
    const today = moment();

    let currentYearMonth = moment().format("YYYY-MM");

    return currentYearMonth;
};

// 获取日(天)期快捷选项
export const getDayPickOptions = () => {
    const shortcuts = [
        {
            text: '今天',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 0)
                return [start, end]
            },
        },
        {
            text: '昨天',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
                return [start, start]
            },
        },
        {
            text: '最近三天',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
                return [start, end]
            },
        },
        {
            text: '最近一周',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                return [start, end]
            },
        },
        {
            text: '最近一个月',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                return [start, end]
            },
        },
    ];
    return shortcuts;
};

// 获取日(时、分、秒)期快捷选项
export const getDatePickOptions = () => {
    const shortcuts = [
        {
            text: '今天',
            value: () => {
                const end = new Date();
                const start = new Date();
                start.setTime(resolveTimeStamp(start.getTime() - 3600 * 1000 * 24 * 0,'start'));
                end.setTime(resolveTimeStamp(end.getTime(),'end'));
                return [start, end]
            },
        },
        {
            text: '昨天',
            value: () => {
                const end = new Date();
                const start = new Date();
                start.setTime(resolveTimeStamp(start.getTime() - 3600 * 1000 * 24 * 1,'start'));
                end.setTime(resolveTimeStamp(end.getTime() - 3600 * 1000 * 24 * 1,'end'));
                return [start, end]
            },
        },
        {
            text: '最近三天',
            value: () => {
                const end = new Date();
                const start = new Date();
                start.setTime(resolveTimeStamp(start.getTime() - 3600 * 1000 * 24 * 3));
                end.setTime(resolveTimeStamp(end.getTime(),'end'));
                return [start, end]
            },
        },{
            text: '最近一周',
            value: () => {
                const end = new Date();
                const start = new Date();
                start.setTime(resolveTimeStamp(start.getTime() - 3600 * 1000 * 24 * 7));
                end.setTime(resolveTimeStamp(end.getTime(),'end'));
                return [start, end]
            },
        },{
            text: '最近一个月',
            value: () => {
                const end = new Date();
                const start = new Date();
                start.setTime(resolveTimeStamp(start.getTime() - 3600 * 1000 * 24 * 30));
                end.setTime(resolveTimeStamp(end.getTime(),'end'));
                return [start, end]
            },
        }
    ];
    return shortcuts;
};

// 获取日期(月)快捷选项
export const getMonthPickOptions = () => {
    const shortcuts = [
        {
            text: '本月',
            value: [new Date(), new Date()],
        },
        {
            text: '上月',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setMonth(start.getMonth() - 1)
                return [start, start]
            }
        }
    ];
    return shortcuts;
};

export const resolveTimeStamp = (timeStamp, type = 'start') => {
    let date = moment(timeStamp).format('YYYY-MM-DD HH:mm:ss');
    const endDate = type === 'start' ? '00:00:00' : '23:59:59';

    let dateArr = date.split(' ');
    dateArr[1] = endDate;

    const newDate = dateArr.join(' ');
    const newTimeStamp = moment(newDate).valueOf();

    return newTimeStamp;
};

export const getParamsTime = (originTime) => {
    const startTimeArr = originTime.split(' ');
    const endTimeArr = startTimeArr[1].split(':');

    const resolveTime = `${startTimeArr[0]}${endTimeArr[0]}`.replaceAll("-", "");
    return resolveTime;
};

// 获取当前时刻时间
export const getCurTime = () => {
    const timeStamp = new Date().getTime();
    const time = moment(timeStamp).format('YYYY-MM-DD HH:mm:ss');
    return time;
};
